package com.naszetatry.registration;

import java.io.IOException;
import java.util.Random;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.naszetatry.beans.User;
import com.naszetatry.controller.RegistrationController;
import com.naszetatry.exception.InvalidLoginException;
import com.naszetatry.exception.InvalidMailException;
import com.naszetatry.exception.InvalidPasswordException;
import com.naszetatry.exception.LoginAlreadyExistsException;
import com.naszetatry.exception.MailAlreadyExistsException;
import com.naszetatry.logic.UserLogic;
import com.naszetatry.mail.MailSender;
/**
 * Serwlet odpowiedzialny za rejestracje uzytkownika.
 * Po nieudanej probie wraca do strony z formularzem wraz z lista bledow.
 * Po udanej rejestracji wysyla maila z kodem aktywacyjnym. 
 * @author Maciej Koch
 *
 */
public class Registration extends HttpServlet {

	private static final long serialVersionUID = 1L;
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		//first visit
		if("1".equals(request.getParameter("do"))) {
			getServletContext().getRequestDispatcher("/jsp/registration.jsp").forward(request, response);
			return;
		}
		//validation
		User user = new User();
		RValidation validation = new RValidation();
		
		String login = request.getParameter("login");
		String password1 = request.getParameter("password1");
		String password2 = request.getParameter("password2");
		String mail = request.getParameter("mail");
		String regulation = request.getParameter("regulation");
		String activationKey = validation.generateActivationKey();

		validation.isRegulationAccepted = validation.isRegulationAccepted(regulation);
		validation.isPasswordMatch = validation.isPasswordMatch(password1, password2);
		
		user.setLogin(login);
		user.setPassword(password1);
		user.setMail(mail);
		user.setActivationKey(activationKey);
	
		validation.isRegulationAccepted = validation.isRegulationAccepted(regulation);
		validation.isPasswordMatch = validation.isPasswordMatch(password1, password2);
		
		if(validation.isRegulationAccepted && validation.isPasswordMatch)
			try {
				UserLogic.register(user);
			} catch (InvalidLoginException e) {
				validation.isLoginValid = false;
			} catch (InvalidPasswordException e) {
				validation.isPasswordValid = false;
			} catch (InvalidMailException e) {
				validation.isMailValid = false;
			} catch (MailAlreadyExistsException e) {
				validation.isMailUnused = false;
			} catch (LoginAlreadyExistsException e) {
				validation.isLoginUnused = false;
			}
				
		boolean registrationAccepted = validation.isLoginValid && validation.isPasswordMatch && validation.isMailValid
									&& validation.isPasswordValid && validation.isRegulationAccepted
									&& validation.isLoginUnused && validation.isMailUnused;
				
		if(!registrationAccepted) {
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/jsp/registration.jsp");
			RegistrationController.setRegisteredUser(request, user);
			RegistrationController.setValidation(request, validation);
			rd.forward(request, response);
		}
		else
		{	

			MailSender mailSender = new MailSender();			
			String subject = "Link aktywacyjny";
			//TODO
			String text = "http://localhost:8080/nasze-tatry/activation?login="+user.getLogin()+"&activationKey="+user.getActivationKey();
			String adress = user.getMail();
		
			boolean hasMailSent = mailSender.send(adress, subject, text);
			
			if(!hasMailSent)
				getServletContext().getRequestDispatcher("/jsp/mailFailed.jsp").forward(request, response);
			else 
				getServletContext().getRequestDispatcher("/jsp/registrationSuccess.jsp").forward(request, response);

		}
	}
	
	
}
